package com.school_admin.util;

import org.jetbrains.annotations.NotNull;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public String handleAllExceptions(Exception ex) {
        // 记录日志，处理异常信息
        return "An error occurred: " + ex.getMessage();
    }

    @NotNull
    @Override
    protected ResponseEntity<Object>
    handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
                                 @NotNull HttpHeaders headers,
                                 @NotNull HttpStatus status,
                                 @NotNull WebRequest request) {
        // 记录日志，处理参数验证异常信息
        return new ResponseEntity<>("Validation failed: " +
                ex.getBindingResult().toString(), HttpStatus.BAD_REQUEST);
    }
}
